{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    }
   ],
   "source": [
    "def knapsack_max_value(n, W, weights, values):\n",
    "    dp = [[0] * (W+1) for _ in range(n+1)]\n",
    "\n",
    "    for i in range(1, n+1):\n",
    "        for j in range(1, W+1):\n",
    "            if weights[i-1] > j:\n",
    "                dp[i][j] = dp[i-1][j]\n",
    "            else:\n",
    "                dp[i][j] = max(dp[i-1][j], values[i-1] +\n",
    "                               dp[i-1][j-weights[i-1]])\n",
    "\n",
    "    return dp[n][W]\n",
    "\n",
    "\n",
    "# 测试数据\n",
    "n = 4\n",
    "W = 8\n",
    "weights = [2, 3, 4, 5]\n",
    "values = [3, 4, 5, 6]\n",
    "\n",
    "print(knapsack_max_value(n, W, weights, values))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
